package com.thiscc.sum.utils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.util.JSON;

/**
 * JSON对象操作工具类，格式转化，获取值（防止空异常）
 * 
 * @author Ron
 * @createTime 2014.08.30
 */
public class DBObjectUtils {

    /***
     * 获取对象中的字符串，出现异常返回""
     * 
     * @param dbobject
     * @param key
     * @return
     */
    public static String getString(DBObject dbobject, String key) {

        if (dbobject == null) {
            return "";
        }
        try {
            return dbobject.get(key).toString();
        } catch (Exception e) {
            return "";
        }
    }

    /***
     * 获取对象中的字符串，出现异常返回""
     * 
     * @param dbobject
     * @param key
     * @return
     */
    public static String getString(JSONObject dbobject, String key) {

        if (dbobject == null) {
            return "";
        }
        try {
            return dbobject.get(key).toString();
        } catch (Exception e) {
            return "";
        }
    }

    /****
     * 获取对象中的日期对象，异常返回Date原始日期
     * 
     * @param dbparam
     * @param key
     * @return
     */
    public static Date getDate(DBObject dbparam, String key) {

        if (dbparam == null) {
            return new Date(1);
        }
        try {
            return (Date) dbparam.get(key);
        } catch (Exception e) {
            return new Date(1);
        }
    }

    /****
     * 获取DBObject中的JSONObject对象，异常返回JSONObject没有值得对象
     * 
     * @param dbobject
     * @param key
     * @return
     */
    public static JSONObject getJSONObject(DBObject dbobject, String key) {

        if (dbobject == null) {
            return new JSONObject();
        }
        try {
            return JSONObject.fromObject(dbobject.get(key));
        } catch (Exception e) {
            return new JSONObject();
        }
    }

    /****
     * 获取DBObject中的list对象，异常返回List没有值的对象
     * 
     * @param dbobject
     * @param key
     * @return 返回list<DBObject>对象，异常则长度为0
     */
    @SuppressWarnings("unchecked")
    public static List<DBObject> getList(DBObject dbobject, String key) {

        if (dbobject == null) {
            return new ArrayList<DBObject>();
        }
        try {
            List<DBObject> list = (List<DBObject>) dbobject.get(key);
            return list == null ? new ArrayList<DBObject>() : list;
        } catch (Exception e) {
            return new ArrayList<DBObject>();
        }
    }

    /****
     * 获取JSONObject中的list对象，异常返回List没有值的对象
     * 
     * @param dbobject
     * @param key
     * @return 返回list<DBObject>对象，异常则长度为0
     */
    @SuppressWarnings("unchecked")
    public static List<DBObject> getList(JSONObject dbobject, String key) {

        if (dbobject == null) {
            return new ArrayList<DBObject>();
        }
        try {
            List<DBObject> list = (List<DBObject>) dbobject.get(key);
            return list == null ? new ArrayList<DBObject>() : list;
        } catch (Exception e) {
            return new ArrayList<DBObject>();
        }
    }

    /****
     * 获取DBObject中的DBlist对象，异常返回DBlist没有值的对象
     * 
     * @param dbobject
     * @param key
     * @return 返回BasicDBList对象，异常则长度为0
     * @author eclipse
     */
    public static BasicDBList getDBList(DBObject dbobject, String key) {

        if (dbobject == null) {
            return new BasicDBList();
        }
        try {
            BasicDBList list = (BasicDBList) dbobject.get(key);
            return list == null ? new BasicDBList() : list;
        } catch (Exception e) {
            return new BasicDBList();
        }
    }

    /****
     * 获取DBObject中的BasicDBObject对象，异常返回BasicDBObject没有值的对象
     * 
     * @param dbobject
     * @param key
     * @return 返回BasicDBObject对象
     * @author eclipse
     */
    public static BasicDBObject getDBObject(DBObject dbobject, String key) {

        if (dbobject == null) {
            return new BasicDBObject();
        }
        try {
            BasicDBObject object = (BasicDBObject) dbobject.get(key);
            return object == null ? new BasicDBObject() : object;
        } catch (Exception e) {
            return new BasicDBObject();
        }
    }

    /****
     * 获取DBObject中的JSONArray对象，异常返回JSONArray没有值的对象
     * 
     * @param dbobject
     * @param key
     * @return 返回JSONArray对象
     * @author eclipse
     */
    public static JSONArray getJSONArray(DBObject dbobject, String key) {

        if (dbobject == null) {
            return new JSONArray();
        }
        try {
            JSONArray object = JSONArray.fromObject(dbobject.get(key));
            return object == null ? new JSONArray() : object;
        } catch (Exception e) {
            return new JSONArray();
        }
    }

    public static int getInt(DBObject dbobject, String key) {

        if (dbobject == null) {
            return 0;
        }
        try {
            Integer object = Integer.parseInt((String) dbobject.get(key));
            return object == null ? 0 : object;
        } catch (Exception e) {
            try {
                Integer object = (Integer) dbobject.get(key);
                return object == null ? 0 : object;
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            return 0;
        }
    }

    /**
     * 将JSONObject 转为DBObject
     * @author helios
     * @param jsonObj
     * @return 异常或为null 这返回null
     * @createtime 2015-1-14
     */
    public static DBObject jsonToDbObject(JSONObject jsonObj) {

        if (jsonObj == null) {
            return null;
        }

        try {

            return (DBObject) JSON.parse(jsonObj.toString());
        } catch (Exception e) {

            return null;
        }
    }
}
